package com.hurt.competition.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hurt.competition.admin.dto.CompetitionReq;
import com.hurt.competition.admin.mapper.CompetitionMapper;
import com.hurt.competition.admin.model.Competition;
import com.hurt.competition.admin.service.CompetitionService;
import com.hurt.competition.common.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class CompetitionServiceImpl extends ServiceImpl<CompetitionMapper, Competition> implements CompetitionService {
    @Override
    public PageResult<Competition> queryList(CompetitionReq req) {
        // 开启分页，只针对遇到的第一个对数据库的查询分页
        Page<Competition> page = PageHelper.startPage(req.getPageNumber(), req.getPageSize());

        LambdaQueryWrapper<Competition> queryWrapper = new LambdaQueryWrapper<>();
        // 模糊查询，like方法的第一个参数为条件，如果为true，则进行后续的操作
        queryWrapper.like(StringUtils.isNotBlank(req.getName()), Competition::getName, req.getName());

        list(queryWrapper);

        // 直接返回page，因为数据在page里封装好了
        PageResult pageResult = new PageResult();
        pageResult.setList(page.getResult());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Override
    public void deploy(Competition competition) {
        save(competition);
    }
}
